﻿#ifndef PLOTTER_H
#define PLOTTER_H

#include <QMap>
#include <QPixmap>
#include <QVector>
#include <QWidget>

QT_BEGIN_NAMESPACE
class QToolButton;
class PlotSettings;
QT_END_NAMESPACE

class Plotter: public QWidget
{
    Q_OBJECT

public:
    Plotter(QWidget *parent = 0);

    void setPlotSettings(const PlotSettings &settings);
    void setCurveData(int id, const QVector<QPointF> &data);
    void clearCurve(int id);
    QSize minimumSizeHint() const;
    QSize sizeHint() const;

public slots:

    void zoomIn();
    void zoomOut();

protected:

    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void wheelEvent(QWheelEvent *event);

private:

    void updateRubberBandRegion();
    void refreashPixmap();
    void drawGrid(QPainter *painter);
    void drawCurves(QPainter *painter);

    enum {Margin = 50};

    QToolButton *zoomInButton;
    QToolButton *zomOutButton;
    QMap<int, QVector<QPointF>> curveMap;
    QVector<PlotSettings> zomStack;
    int curZoom;
    bool rubberBandIsShown;
    QRect rubberBandRect;
    QPixmap pixmap;
};

class PlotSettings
{
public:
    plotSettings();

    void scroll(int dx, int dy);
    void adjust();
    double spanX() const
    {
        return maxX - minX;
    }
    double spanY() const
    {
        return maxY - minY;
    }
    double minX;
    double maxX;
    int numXTicks;
    double minY;
    double maxY;
    int numYTicks;
}


#endif
